﻿using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Oracle.DataAccess.Client;

namespace YXYLCommon
{

    /// <summary>
    /// 保存要执行的存储过程的名称,参数
    /// </summary>
    public class DbProcObject
    {
        private DbParameter createPara(string name,object value)
        {
            if (DbHelper.DbType == eDbType.SqlServer.ToString())
            {
                return new SqlParameter(name,value);
            }
            else if (DbHelper.DbType == eDbType.Oracle.ToString())
            {
                return new OracleParameter(name, value);
            }
            else
            {
                return null;
            }
        }

        public string ProcName;
        public List<DbParameter> ProcParameters = new List<DbParameter>();

        public void Init(string procName)
        {
            this.ProcParameters.Clear();
            this.ProcName = procName;
        }
        public void Init()
        {
            this.ProcParameters.Clear();
        }

        public void AddParameter(string name, object value)
        {
            DbParameter parm = createPara(name,value);
            this.ProcParameters.Add(parm);
        }
        public void AddSQLParameter(string name, object value)
        {
            this.ProcParameters.Add(new SqlParameter(name,value));
        }

        public void AddParameter(string name, object value,DbType dbtype)
        {
            DbParameter parm = createPara(name, value);
            parm.DbType = dbtype;
            this.ProcParameters.Add(parm);
        }
        public void AddParameterOracle(string name, object value, OracleDbType dbtype)
        {
            OracleParameter parm = new OracleParameter(name, dbtype);
            parm.Value = value;
            this.ProcParameters.Add(parm);
        }
       
        public void AddParameterOut(string name,object value,int size)
        {
            DbParameter parm = createPara(name, value);
            parm.Size = size;
            parm.Direction = ParameterDirection.Output;
            this.ProcParameters.Add(parm);
        }
        public void AddSQLParameterOut(string name, object value, int size)
        {
            DbParameter parm = new SqlParameter();
            parm.ParameterName = name;
            parm.Value = value;
            parm.Size = size;
            parm.Direction = ParameterDirection.Output;
            this.ProcParameters.Add(parm);
        }

        public void AddParameterReturn()
        {
            DbParameter parm = createPara("ReturnValue", 0);
            parm.Direction = ParameterDirection.ReturnValue;
            this.ProcParameters.Add(parm);
        }

        public void AddOracleCursor() {
            if (DbHelper.DbType == eDbType.Oracle.ToString())
            {
                OracleParameter parm = new OracleParameter("SMENU_CURSOR", OracleDbType.RefCursor, ParameterDirection.Output);
                this.ProcParameters.Add(parm);
            }
        }
        public void AddOracleReturnParam()
        {
            if (DbHelper.DbType == eDbType.Oracle.ToString())
            {
                this.ProcParameters.Add(new OracleParameter("V_ReturnNo", OracleDbType.Int32, ParameterDirection.Output));
                this.ProcParameters.Add(new OracleParameter("V_ReturnMsg", OracleDbType.Varchar2, ParameterDirection.Output));
            }
        }

        public DbParameter GetParameter(string name)
        {
            for (int i = 0; i < this.ProcParameters.Count;i++ )
            {
                if(this.ProcParameters[i].ParameterName==name)
                {
                    return this.ProcParameters[i];
                }
            }
            return null;
        }
        public object GetParameterValue(string name)
        {
            for (int i = 0; i < this.ProcParameters.Count; i++)
            {
                if (this.ProcParameters[i].ParameterName == name)
                {
                    return this.ProcParameters[i].Value;
                }
            }
            return null;
        }

    }
}
